import turtle
import math

# 棋盘尺寸
BOARD_SIZE = 7

# 棋盘初始化
board = [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
board[0] = ['@', '-', '-', '@', '-', '-', '@']
board[1] = ['|', '@', '-', '@', '-', '@', '|']
board[2] = ['|', '|', '@', '@', '@', '|', '|']
board[3] = ['@', '@', '@', ' ', '@', '@', '@']
board[4] = ['|', '|', '@', '@', '@', '|', '|']
board[5] = ['|', '@', '-', '@', '-', '@', '|']
board[6] = ['@', '-', '-', '@', '-', '-', '@']

# 第二个棋盘初始化
board2 = [[' ' for _ in range(BOARD_SIZE)] for _ in range(BOARD_SIZE)]
board2[0] = [' ', '@', '-', '-', '@', '-', '-', '@']
board2[1] = [' ', '|', '@', '-', '@', '-', '@', '|']
board2[2] = [' ', '|', '|', '@', '@', '@', '|', '|']
board2[3] = [' ', '@', '@', '@', ' ', '@', '@', '@']
board2[4] = [' ', '|', '|', '@', '@', '@', '|', '|']
board2[5] = [' ', '|', '@', '-', '@', '-', '@', '|']
board2[6] = [' ', '@', '-', '-', '@', '-', '-', '@']

    
# 输出当前棋盘状态
def print_board():
    print('  ' + ' '.join([chr(ord('A') + i) for i in range(BOARD_SIZE)]) + ' ' + ' '.join([chr(ord('A') + i) for i in range(BOARD_SIZE)]))
    for i in range(BOARD_SIZE):
        print(str(i + 1) + ' ' + ' '.join(board[i]) + ' ' + ' '.join(board2[i]))

# 落子
def place_piece(row, col, piece):
    boardptr = board2 if col >= BOARD_SIZE else board
    col_adjusted = col - BOARD_SIZE if col >= BOARD_SIZE else col

    error_code = check_valid_position(row, col_adjusted)
    if error_code == -1:
        raise ValueError('无效的落子位置')
    elif error_code == 1:
        raise ValueError('该位置已有棋子')

    boardptr[row][col_adjusted] = piece
    sync_mirror_board(row, col)  # 同步镜像棋盘
    return True

# 吃子
def capture_piece(row, col):
    boardptr = board2 if col >= BOARD_SIZE else board
    col_adjusted = col - BOARD_SIZE if col >= BOARD_SIZE else col

    error_code = check_valid_position(row, col_adjusted)
    if error_code != 0:  # 如果返回的错误代码不为0，即不是空棋位
        return False

    boardptr[row][col_adjusted] = '@'
    sync_mirror_board(row, col)  # 同步镜像棋盘
    return True

# 移动棋子
def move_piece(src_row, src_col, dst_row, dst_col):
    src_board = board2 if src_col >= BOARD_SIZE else board
    dst_board = board2 if dst_col >= BOARD_SIZE else board
    src_col_adjusted = src_col - BOARD_SIZE if src_col >= BOARD_SIZE else src_col
    dst_col_adjusted = dst_col - BOARD_SIZE if dst_col >= BOARD_SIZE else dst_col

    src_error_code = check_valid_position(src_row, src_col_adjusted)
    dst_error_code = check_valid_position(dst_row, dst_col_adjusted)
  
    if src_error_code != 1 or dst_error_code != 0:  # 如果源位置不是棋子或目标位置不是空棋位
        return False

    if src_board[src_row][src_col_adjusted] != 'X' and src_board[src_row][src_col_adjusted] != 'O':
        return False

    if src_board is not dst_board:
        raise ValueError('不能跨棋盘移动棋子')

    dst_board[dst_row][dst_col_adjusted] = src_board[src_row][src_col_adjusted]
    src_board[src_row][src_col_adjusted] = '@'
    sync_mirror_board(src_row, src_col)  # 同步镜像棋盘
    sync_mirror_board(dst_row, dst_col)  # 同步镜像棋盘
    return True

# 棋子合法性检测
def check_valid_position(row, col):
    if col >= BOARD_SIZE:
        boardptr = board2
        col_adjusted = col - BOARD_SIZE
    else:
        boardptr = board
        col_adjusted = col

    if row < 0 or row >= BOARD_SIZE or col_adjusted < 0 or col_adjusted >= BOARD_SIZE:
        return -1  # 无效棋位，请输入有效棋位。
    elif boardptr[row][col_adjusted] == "@":
        return 0  # 该位置是空棋位。
    elif boardptr[row][col_adjusted] == "X" or boardptr[row][col_adjusted] == "O":
        return 1  # 该位置有子。

    return -1  # 无效棋位，请输入有效棋位。


# 同步镜像棋盘
def sync_mirror_board(row, col):
    mirror_positions = [(1, 1), (1, 3), (1, 5), (3, 5), (5, 5), (5, 3), (5, 1), (3, 1)]
    src_board = board2 if col >= BOARD_SIZE else board
    dst_board = board2 if col < BOARD_SIZE else board
    col_adjusted = col - BOARD_SIZE if col >= BOARD_SIZE else col

    for i in range(len(mirror_positions)):
        mirror_row, mirror_col = mirror_positions[i]
        if row == mirror_row and col_adjusted == mirror_col:
            dst_board[mirror_row][mirror_col] = src_board[mirror_row][mirror_col]

            
